package com.tankbattle.stage;

import com.tankbattle.stage.body.StageBody;
import com.tankbattle.stage.body.map.Stage1;
import com.tankbattle.stage.body.map.Stage2;
import com.tankbattle.stage.body.map.Stage3;
import com.tankbattle.stage.body.map.StageRandom;
import com.tankbattle.start.StartBackground;
import com.tankbattle.start.StartPage;
import com.tankbattle.utils.GetResource;

import javax.swing.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;


/**
 * 切换游戏关卡和显示记分榜
 */
public class Stage extends JFrame implements Runnable, GetResource {

    public static ImageIcon stage1Image = new ImageIcon(Stage.class.getClassLoader().getResource("img/stage1.png"));
    public static ImageIcon stage2Image = new ImageIcon(Stage.class.getClassLoader().getResource("img/stage2.png"));
    public static ImageIcon stage3Image = new ImageIcon(Stage.class.getClassLoader().getResource("img/stage3.png"));
    public static Boolean gameOver = false;
    private JPanel backgroundPanel;
    private StageBody stagePanel;
    private StageScore score;
    private StartPage startPage;
    private int stage;
    private int totalStage;
    private int totalPts;

    /**
     * 这里可以优化一下进行跳关
     */
    public Stage(StartPage start) {
        // 第一关开始
        this.stage = 1;
        //共三关
        this.totalStage = 10;
        this.totalPts = 0;
        gameOver = false;
        this.startPage = start;

        backgroundPanel = new BackgroundPanel(stage1Image);
        StageBody.map = new Stage1();
        stagePanel = new StageBody();
        this.stagePanel.setEnemyTanksAward(1);
        this.stagePanel.setEnemyTanksNumberLimit(7);
        this.stagePanel.setEnemyTanksFatty(3);

        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        AudioClip bgm = Applet.newAudioClip(getResource("bgm/gameStart.wav"));
        bgm.play();
        this.add(backgroundPanel);
        this.setResizable(false);
        this.setTitle("Stage 1");
        this.setIconImage(new ImageIcon(getResource("img/symbol.png")).getImage());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(520 + 79, 520 + 28);
        //this.setLocation(screenSize.width / 2 - this.getWidth() / 2
        //        , screenSize.height / 2 - this.getWidth() / 2);
        this.setVisible(true);
    }

    public JPanel getBackgroundPanel() {
        return backgroundPanel;
    }

    public void init() {
        this.remove(backgroundPanel);
        this.setLayout(null);
        stagePanel.setBounds(0, 0, 520, 520);
        this.add(stagePanel);
        score = new StageScore(this.stage, 2, this.stagePanel.getEnemyTanksNumberLimit());
        score.setBounds(520, 0, 79, 520);
        this.add(score);
        this.setSize(520 + 79, 520 + 28);
        this.addKeyListener(stagePanel);
        new Thread(stagePanel).start();
        if (stage == 1) {
            new Thread(this).start();
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.score.setStage(this.stage);
            this.score.setAliveEnemy(this.stagePanel.getEnemyTanksNumberLimit());
            if (this.stagePanel.getPlayerTank().getLife() <= 0) {
                this.score.setPlayerLife(0);
            } else {
                this.score.setPlayerLife(this.stagePanel.getPlayerTank().getLife());
            }
            this.score.repaint();

            if (this.stagePanel.getGameOver() != null || this.stagePanel.getEnemyTanksNumberLimit() == -1) {
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.stagePanel.getPlayerTank().setMoving(false);
                this.remove(this.score);
                this.remove(this.stagePanel);
                GameScores g = new GameScores(this.stagePanel.getScore().getNormalTank(), this.stagePanel.getScore().getFastTank()
                        , this.stagePanel.getScore().getFattyTank(), this.stagePanel.getScore().getTool(), stage, 0);
                g.setBounds(0, 0, 599, 520);
                this.add(g);
                this.repaint();
                this.totalPts += g.getTotalPts();
                if (this.stagePanel.getGameOver() != null) {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    this.remove(g);
                    this.add(new GameOver());
                    gameOver = true;
                    this.repaint();
                    AudioClip bgm = Applet.newAudioClip(getResource("bgm/gameOver.wav"));
                    bgm.play();
                    try {
                        Thread.sleep(4000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    this.setVisible(false);
                    this.startPage.setLocation(this.getX(), this.getY());
                    this.startPage.setVisible(true);
                    ((StartBackground) this.startPage.getBackgroundPanel()).setPts(this.totalPts);
                    this.startPage.repaint();
                    StageBody.homeAlive = true;
                    StageBody.power = null;
                    this.stagePanel.setGameOver(null);
                } else if (this.stagePanel.getEnemyTanksNumberLimit() == -1) {
                    // 赢了就跳下一关
                    try {
                        Thread.sleep(4000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    this.stage++;
                    if (stage > totalStage) {
                        break;
                    }
                    this.setTitle("Stage " + stage);
                    AudioClip bgm = Applet.newAudioClip(getResource("bgm/gameStart.wav"));
                    bgm.play();

                    this.remove(g);
                    switch (this.stage) {
                        case 1:
                            this.backgroundPanel = new BackgroundPanel(stage1Image);
                            break;
                        case 2:
                            this.backgroundPanel = new BackgroundPanel(stage2Image);
                            break;
                        case 3:
                            this.backgroundPanel = new BackgroundPanel(stage3Image);
                            break;
                        default:
                            break;
                    }
                    this.backgroundPanel.setBounds(0, 0, 599, 520);
                    this.add(backgroundPanel);
                    this.repaint();
                    try {
                        Thread.sleep(3500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    switch (this.stage) {
                        case 1:
                            StageBody.map = new Stage1();
                            this.stagePanel = new StageBody();
                            this.stagePanel.setEnemyTanksAward(3);
                            this.stagePanel.setEnemyTanksFatty(3);
                            this.stagePanel.setEnemyTanksNumberLimit(9);
                            break;
                        case 2:
                            StageBody.map = new Stage2();
                            this.stagePanel = new StageBody();
                            this.stagePanel.setEnemyTanksAward(3);
                            this.stagePanel.setEnemyTanksFatty(5);
                            this.stagePanel.setEnemyTanksNumberLimit(11);
                            break;
                        case 3:
                            StageBody.map = new Stage3();
                            this.stagePanel = new StageBody();
                            this.stagePanel.setEnemyTanksAward(3);
                            this.stagePanel.setEnemyTanksFatty(7);
                            this.stagePanel.setEnemyTanksNumberLimit(15);
                            break;
                        default:
                            StageBody.map = new StageRandom();
                            this.stagePanel = new StageBody();
                            this.stagePanel.setEnemyTanksAward(5);
                            this.stagePanel.setEnemyTanksFatty(10);
                            this.stagePanel.setEnemyTanksNumberLimit(30);
                            break;
                    }
                    StageBody.power = null;
                    init();
                    try {
                        Thread.sleep(1000);
                        StageBody.bcgbgm.loop();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }

}
